import { Util } from "../../utils/util";
import { Engine } from "../engine";
import { WareHouse } from "../inventory/warehouse";
import { SubCommodity } from "./subCommodity";
//import { CommodityKindUnit } from "../basicData/commodityKindUnit";

/// 过滤条件
/// Filter conditions
export class SubCommodityInventorySumFilter {
    /**
     * @param {Object} options
     * @param {string} [options.storeId] - Store ID
     */
    constructor({ storeId = null } = {}) {
        /// 规格商品编号
        /// Commodity specification code
        this.code = null;

        /// 规格商品名称
        /// Commodity specification name
        this.subCommodityName = null;

        /// 规格名称
        /// Specification name
        this.standName = null;

        /// 条码二维码
        /// Barcode/QR code
        this.qrCode = null;

        /// 全匹配二维码
        /// Full match QR code
        this.fullMatchQRCode = null;

        /// 类别 CommodityCategory
        /// Category
        this.category = null;

        /// 仓库 WareHouse
        /// Warehouse
        this.wareHouse = null;

        /// 固定仓库
        /// Fixed warehouse
        this.blockWareHouse = false;

        /// 启用状态
        /// Enabled status
        this.stateFilter = true;

        /// 是否查询库存成本 todo 这个dart 中是 int 类型
        /// Only enabled in inventory query mode, not for commodity selection
        /// Associated with multiple tables, slow performance
        this.needCostData = null;

        /// 门店过滤
        /// Store filter
        this.storeId = storeId || Engine.profile?.filterStore?.id;
    }

    clear() {
        this.code = null;
        this.subCommodityName = null;
        this.standName = null;
        this.qrCode = null;
        this.category = null;
        if (!this.blockWareHouse) {
            this.wareHouse = null;
        }
        this.stateFilter = true;
    }

    /**
     * Clone filter
     * @param {SubCommodityInventorySumFilter} filter 
     * @returns {SubCommodityInventorySumFilter}
     */
    static clone(filter) {
        const cloned = new SubCommodityInventorySumFilter();
        cloned.code = filter.code;
        cloned.subCommodityName = filter.subCommodityName;
        cloned.standName = filter.standName;
        cloned.qrCode = filter.qrCode;
        cloned.category = filter.category;
        cloned.wareHouse = filter.wareHouse;
        cloned.blockWareHouse = filter.blockWareHouse;
        cloned.stateFilter = filter.stateFilter;
        cloned.needCostData = filter.needCostData;
        cloned.storeId = filter.storeId;
        return cloned;
    }

    toJson() {
        const ret = {};

        if (this.code && this.code.length > 0) {
            ret["c_lke_commodityCode"] = this.code;
        }
        if (this.subCommodityName && this.subCommodityName.length > 0) {
            ret["c_lke_commodityName"] = this.subCommodityName;
        }
        if (this.standName && this.standName.length > 0) {
            ret["c_lke_specName"] = this.standName;
        }
        if (this.qrCode && this.qrCode.length > 0) {
            ret["c_lke_commodityBarcode"] = this.qrCode;
        }
        if (this.fullMatchQRCode && this.fullMatchQRCode.length > 0) {
            ret["c_leq_commodityBarcode"] = this.fullMatchQRCode;
        }
        if (this.category) {
            ret["c_commodityCategoryId"] = this.category.id;
        }
        if (this.stateFilter != null) {
            ret["c_leq_useState"] = this.stateFilter ? 1 : 2;
        }

        const retMap = {};
        if (Object.keys(ret).length > 0) {
            for (const [key, value] of Object.entries(ret)) {
                retMap["exts." + key] = value;
            }
        }
        if (this.wareHouse) {
            retMap['storageId'] = this.wareHouse.id;
        }
        if (this.needCostData != null) {
            retMap['needCostData'] = this.needCostData;
        }
        if (this.storeId) {
            retMap["storeId"] = this.storeId;
        }

       // console.log("SubCommodityInventorySumFilter toJson", retMap);

        return retMap;
    }
}

/// 库存汇总
/// Inventory summary data
export class InventorySumData {
    /**
     * @param {number} totalCount - Total quantity
     * @param {number} totalCost - Total cost
     */
    constructor(totalCount, totalCost) {
        /// 总数量
        /// Total quantity
        this.totalCount = totalCount;

        /// 总成本
        /// Total cost
        this.totalCost = totalCost;
    }
}

/// 商品库存信息
/// Commodity inventory information
export class SubCommodityInventorySumData {
    /**
     * @param {Object} options
     * @param {SubCommodity} options.subCommodity - Sub commodity
     * @param {number} options.singleCost - Single cost
     * @param {number} options.totalCost - Total cost
     * @param {number} options.availableCount - Available count
     * @param {number} options.waitInCount - Wait in count
     * @param {number} options.waitOutCount - Wait out count
     * @param {number} options.totalInventoryCount - Total inventory count
     */
    constructor({
        subCommodity,  // 子商品
        singleCost,  // 单品成本
        totalCost,  // 累计成本
        availableCount, // 可用库存
        waitInCount, // 待入库
        waitOutCount, // 待出库
        totalInventoryCount //  当前库存 = availableCount + waitOutCount
    }) {
        /// 商品规格
        /// Commodity specification
        this.subCommodity = subCommodity;

        /// 单品成本
        /// Single item cost
        this.singleCost = singleCost;

        /// 累计成本
        /// Accumulated cost
        this.totalCost = totalCost;

        /// 可用库存
        /// Available inventory
        this.availableCount = availableCount;

        /// 待入库
        /// Pending inbound
        this.waitInCount = waitInCount;

        /// 待出库
        /// Pending outbound
        this.waitOutCount = waitOutCount;

        /// 当前库存 = availableCount + waitOutCount
        /// Current inventory = availableCount + waitOutCount
        this.totalInventoryCount = totalInventoryCount;

        /// 自动化测试临时变量
        /// Temporary variable for automated testing
        this.orgSumDataForTest = null;

        /// 自动化测试临时变量
        /// Temporary variable for automated testing
        this.inventory = null;
    }

    /**
     * Clone data
     * @param {SubCommodityInventorySumData} data 
     * @returns {SubCommodityInventorySumData}
     */
    static clone(data) {
        return new SubCommodityInventorySumData({
            subCommodity: data.subCommodity,
            singleCost: data.singleCost,
            totalCost: data.totalCost,
            availableCount: data.availableCount,
            waitInCount: data.waitInCount,
            waitOutCount: data.waitOutCount,
            totalInventoryCount: data.totalInventoryCount
        });
    }

    /**
     * Create from JSON
     * @param {Object} json 
     * @returns {SubCommodityInventorySumData}
     */
    static fromJson(json) {
        //console.log('SubCommodityInventorySumData.fromJson:',json)
        const subCommodity = SubCommodity.fromJson(json);
        subCommodity.id = Util.jsonToString(json["specId"]);

        const singleCost = Util.jsonToDouble(json['allStoragePrice'], 0);
        const totalInventoryCount = Util.jsonToDouble(json['sumCount'], 0);

        return new SubCommodityInventorySumData({
            subCommodity: subCommodity,
            singleCost: singleCost,
            totalCost: singleCost * totalInventoryCount,
            availableCount: Util.jsonToDouble(json['canUseStore'], 0),
            waitInCount: Util.jsonToDouble(json['waitInStore'], 0),
            waitOutCount: Util.jsonToDouble(json['waitOutStore'], 0),
            totalInventoryCount: totalInventoryCount
        });
    }

    /**
     * Check equality with another instance
     * @param {SubCommodityInventorySumData} instance 
     * @returns {boolean}
     */
    equal(instance) {
        return this.subCommodity.id === instance.subCommodity.id;
    }

    /**
     * Convert to print-friendly JSON
     * @returns {Object}
     */
    toPrintJson() {
        return {
            'subCommodity': this.subCommodity.name,
            "subCommodityId": this.subCommodity.id,
            'singleCost': this.singleCost,
            "totalCost": this.totalCost,
            'availableCount': this.availableCount,
            'waitInCount': this.waitInCount,
            'waitOutCount': this.waitOutCount,
            'totalInventoryCount': this.totalInventoryCount,
            'totalCost': this.totalCost
        };
    }
}